package cn.itsource;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTest {

     /*
        规律：
            使用redisTemplate进行数据存储时，
                存和取数据时，使用的方法是同一套
                    存字符串：opsForValue()
                    存list:  opsForList()
                    存set:   opsForSet()
                    存hash:  opsForHash()
        StringRedisTemplate:
            它是RedisTemplate的一个子类
            存储数据时，key和value都必须是string
     */
     @Autowired
    private StringRedisTemplate redisTemplate;

    @Test
    public void testSetString(){
        redisTemplate.opsForValue().set("company","源码");

    }
    @Test
    public void testGetString(){
        String company = redisTemplate.opsForValue().get("company");
        System.out.println(company);
    }

    @Test
    public void setTime(){
        redisTemplate.opsForValue().set("myage","22",10, TimeUnit.SECONDS);
        String myage = redisTemplate.opsForValue().get("myage");
        System.out.println(myage);


    }
}
